// 对二进制文件进行随机读取
#include <stdio.h>
#include <stdlib.h>
#define LEN 12
typedef struct point
{
	int x, y;
	char name;
};

int main()
{
	/*******************code*******************/
	FILE *fp;
	/*create point_array */
	point p_set[LEN];
	for (int i = 0; i < LEN; i++)
	{
		p_set[i].x = p_set[i].y = i;
		p_set[i].name = 'p';
	}

	if ((fp = fopen("test_binary", "wb+")) == NULL)
		exit(1);

	/********************************* */
	int i = fwrite(p_set, sizeof(point), LEN, fp);
	/********************************* */

	fseek(fp, 0, SEEK_END);
	int size = ftell(fp) / sizeof(point);
	printf("total element:%d\nplease switch index which you want to look:\n", size);

	int index = 0;
	point p1;
	while (scanf("%d", &index) == 1 && index > 0 && index < size)
	{
		fseek(fp, index * sizeof(point), SEEK_SET);
		fread(&p1, sizeof(point), 1, fp);
		printf("%d,%d,%c\n", p1.x, p1.y, p1.name);
		printf("please switch index which you want to look:\n");
	}

	/********************************** */
	/********************************** */
	// 61 62 63 64
	fclose(fp);
	/******************************************/
	printf("\n\n****************************\n");
	printf("Press Enter key to continue\n");
	getchar();
	return 0;
	/******************************************/
}
